package com.bkav.portal.portlet.product;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.bkav.portal.portlet.product.model.Order;
import com.bkav.portal.portlet.product.model.ProductCategory;
import com.bkav.portal.portlet.product.model.impl.ProductImpl;
import com.bkav.portal.portlet.product.service.OrderLocalServiceUtil;
import com.bkav.portal.portlet.product.service.Order_LocalServiceUtil;
import com.bkav.portal.portlet.product.service.ProductCategoryLocalService;
import com.bkav.portal.portlet.product.service.ProductCategoryLocalServiceUtil;
import com.bkav.portal.portlet.product.service.ProductLocalServiceUtil;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.util.Portal;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class ProductAdminPortlet
 */
public class ProductAdminPortlet extends MVCPortlet {

	public void updateProductCategory(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException, IOException {

		long companyId = PortalUtil.getCompanyId(actionRequest);

		long groupId = PortalUtil.getScopeGroupId(actionRequest);

		long userId = PortalUtil.getUserId(actionRequest);

		String userName = PortalUtil.getUserName(userId, StringPool.BLANK);

		long categoryId = ParamUtil.getLong(actionRequest, "categoryId", 0L);

		String categoryName = ParamUtil
				.getString(actionRequest, "categoryName");

		String categoryInfo = ParamUtil
				.getString(actionRequest, "categoryInfo");

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");

		if (categoryId > 0) {// Update category

			ProductCategoryLocalServiceUtil.updateProductCategory(companyId,
					groupId, userId, userName, categoryId, categoryName,
					categoryInfo);
		} else {// Add category

			ProductCategoryLocalServiceUtil.addProductCategory(companyId,
					groupId, userId, userName, categoryName, categoryInfo);
		}

		if (Validator.isNotNull(redirectURL)) {
			actionResponse.sendRedirect(redirectURL);
		}

	}

	public void deleteProductCategory(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException, IOException {

		long categoryId = ParamUtil.getLong(actionRequest, "categoryId", 0L);

		boolean deleteProductCategory = ParamUtil.getBoolean(actionRequest,
				"deleteProductCategory", false);

		if (categoryId > 0) {

			ProductCategoryLocalServiceUtil.deleteProductCategory(categoryId,
					deleteProductCategory);
		}

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");

		if (Validator.isNotNull(redirectURL)) {
			actionResponse.sendRedirect(redirectURL);
		}
	}

	public void updateProduct(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException, IOException {

		long companyId = PortalUtil.getCompanyId(actionRequest);
		long groupId = PortalUtil.getScopeGroupId(actionRequest);
		long userId = PortalUtil.getUserId(actionRequest);
		String userName = PortalUtil.getUserName(userId, StringPool.BLANK);

		long productId = ParamUtil.getLong(actionRequest, "productId", 0L);

		long categoryId = ParamUtil.getLong(actionRequest, "categoryId");

		String productName = ParamUtil.getString(actionRequest, "productName");

		String productInfo = ParamUtil.getString(actionRequest, "productInfo");

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");

		if (productId > 0) {// Update category

			ProductLocalServiceUtil.updateProduct(companyId, groupId, userId,
					userName, productId, categoryId, productName, productInfo);
		} else {// Add category

			ProductLocalServiceUtil.addProduct(companyId, groupId, userId,
					userName, categoryId, productName, productInfo);
		}

		if (Validator.isNotNull(redirectURL)) {
			actionResponse.sendRedirect(redirectURL);
		}

	}

	public void deleteProduct(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException, IOException {

		long productId = ParamUtil.getLong(actionRequest, "productId", 0L);

		boolean deleteProduct = ParamUtil.getBoolean(actionRequest,
				"deleteProduct", false);

		if (productId > 0) {

			ProductLocalServiceUtil.deleteProduct(productId);
		}

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");

		if (Validator.isNotNull(redirectURL)) {
			actionResponse.sendRedirect(redirectURL);
		}
	}

	public void updateOrder(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException, IOException { 
		try {
		long companyId = PortalUtil.getCompanyId(actionRequest);

		long groupId = PortalUtil.getScopeGroupId(actionRequest);

		long userId = PortalUtil.getUserId(actionRequest);

		String userName = PortalUtil.getUserName(userId, StringPool.BLANK);

		long productId = ParamUtil.getLong(actionRequest, "productId");

		String email = ParamUtil.getString(actionRequest, "email");

		String orderSL = ParamUtil.getString(actionRequest, "SL");

		Order_LocalServiceUtil.addOrder(companyId, groupId, userId, userName,
				productId, email, orderSL);

		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		SessionMessages.add(actionRequest, "Order-save-done");
		if (Validator.isNotNull(redirectURL)) {

			actionResponse.sendRedirect(redirectURL);
		}
		}
		catch (Exception e) {
			    e.printStackTrace();
				SessionErrors.add(actionRequest, "save-failed");
				
		}
	}

	

}
